<template>
  <div class="login">
    <section class="login-box animated flipInY flex-j-c">
      <!-- 左侧 -->
      <div class="login-left" v-if="device === 'pc'">
        <img class="login-img" src="@/assets/images/banner-BqOQ-fEP.png" />
      </div>
      <!-- 右侧 -->
      <div class="login-right">
        <a-form
          ref="FormRef"
          :model="form"
          :style="{ width: '84%' }"
          :label-col-style="{ display: 'none' }"
          :wrapper-col-style="{ flex: 1 }"
        >
          <h3 class="login-form-title"><img class="logo" src="@/assets/images/logo.png" /><span>买手财务管理</span></h3>
          <a-form-item field="username" :rules="[{ required: true, message: '请输入账号' }]">
            <a-input v-model="form.username" placeholder="账号" size="medium" allow-clear>
              <template #prefix><icon-user :stroke-width="1" :style="{ fontSize: '20px' }" /></template>
            </a-input>
          </a-form-item>
          <a-form-item field="password" :rules="[{ required: true, message: '请输入密码' }]">
            <a-input-password v-model="form.password" placeholder="密码" size="medium" @press-enter="login" allow-clear>
              <template #prefix><icon-lock :stroke-width="1" :style="{ fontSize: '20px' }" /></template>
            </a-input-password>
          </a-form-item>
          <!-- <a-form-item field="captcha" :rules="[{ required: true, message: '请输入验证码' }]">
            <a-input placeholder="验证码" v-model="form.captcha" @press-enter="login">
              <template v-slot:append>
                <img class="h-30 point" :src="captchaInfo.image_content" @click="getCaptcha" />
              </template>
            </a-input>
          </a-form-item> -->
          <a-form-item>
            <a-row justify="space-between" align="center" style="width: 100%">
              <a-checkbox v-model="remember">记住密码</a-checkbox>
              <a-link @click="Notification.warning('请联系管理员')">忘记密码</a-link>
            </a-row>
          </a-form-item>
          <a-form-item>
            <a-space direction="vertical" fill style="width: 100%">
              <a-button type="primary" size="large" long :loading="loading" @click="login">登录</a-button>
              <!-- <a-button type="text" size="large" long class="register-btn" @click="Notification.warning('请联系管理员')"
                >注册账号</a-button
              > -->
            </a-space>
          </a-form-item>
        </a-form>
      </div>
    </section>
    <!-- <GiThemeBtn class="theme-btn"></GiThemeBtn> -->
    <LoginBg></LoginBg>
  </div>
</template>

<script setup>
import { useUserStore } from '@/store'
import { useLoading } from '@/hooks'
import { Message, Notification } from '@arco-design/web-vue'
import LoginBg from './components/LoginBg/index.vue'
import { captcha } from '@/apis'
import Crypto from '@/utils/encryp.js'
const key = 'e4ea89835ad64eb1b8d76069e33908d4'
const iv = 'ff465fdecc764337'

defineOptions({ name: 'Login' })
const router = useRouter()
const userStore = useUserStore()
const device = ref('pc')
const is_mobi =
  navigator.userAgent
    .toLowerCase()
    .match(/(ipod|ipad|iphone|android|coolpad|mmp|smartphone|midp|wap|xoom|symbian|j2me|blackberry|wince)/i) != null
device.value = is_mobi ? 'mobile' : 'pc'

const form = reactive({
  guard: 'admin',
  captcha_key: '',
  captcha: '',
  username: '',
  password: ''
})

// 记住密码
const remember = ref(true)
// 登录加载
const { loading, setLoading } = useLoading()
const errorMessage = ref('')

const FormRef = ref()

const captchaInfo = ref({
  expired_at: '',
  image_content: '',
  key: ''
})

const loginInfoO = localStorage.getItem('loginInfo')
if (loginInfoO) {
  const loginInfo = JSON.parse(Crypto.decrypt(loginInfoO, key, iv))
  form.username = loginInfo.username
  form.password = loginInfo.password
}

// 获取验证码图片
const getCaptcha = async () => {
  const res = await captcha()
  captchaInfo.value = { ...res.data }
  form.captcha_key = captchaInfo.value.key
}
// getCaptcha()

// 点击登录
const login = async () => {
  try {
    const flag = await FormRef.value?.validate()
    if (flag) return
    setLoading(true)
    await userStore.login(form)
    router.push('/index')
    setLoading(false)
    if (remember.value) {
      const loginInfo = Crypto.encrypt(
        JSON.stringify({
          username: form.username,
          password: form.password
        }),
        key,
        iv
      )
      localStorage.setItem('loginInfo', loginInfo)
    } else {
      localStorage.setItem('loginInfo', '')
    }
    Message.success('登录成功')
  } catch (error) {
    getCaptcha()
    errorMessage.value = error.message
  } finally {
    setLoading(false)
  }
}
</script>

<style lang="scss" scoped>
.register-btn,
.register-btn:hover {
  color: var(--color-text-2);
}

.login-form-title {
  color: var(--color-text-1);
  font-weight: 500;
  font-size: 20px;
  line-height: 32px;
  margin-bottom: 20px;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  .logo {
    width: 32px;
    height: 32px;
    margin-right: 6px;
  }
}

.theme-btn {
  position: fixed;
  top: 20px;
  left: 30px;
  z-index: 9999;
}

.login {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: var(--color-bg-5);
  &-box {
    height: 380px;
    display: flex;
    z-index: 999;
    box-shadow: 0 2px 4px 2px rgba(0, 0, 0, 0.08);
  }
}

.login-left {
  width: 450px;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  background: linear-gradient(60deg, rgb(var(--primary-6)), rgb(var(--primary-3)));
  .login-img {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    transition: all 0.3s;
    object-fit: cover;
  }
}

.login-right {
  width: 270px;
  height: 100%;
  background: var(--color-bg-1);
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 30px;
  box-sizing: border-box;
}

:deep(.arco-input-append) {
  padding: 0 !important;
}

.point {
  cursor: pointer;
}
</style>
